<?php

	function ValidatePageInput( $page_base )
	{
		
		
		eval( 'global $v' . "_$page_base;" );
		
		$re = VALIDATION_OK;
		
		$v = "v_" . $page_base;
		
		$errmsg = GetMessageStack();
		
		if( !isset($$v) )
			return $re;
			
		foreach( $$v as $key => $itemVector )
		{
			if( $itemVector['pre'] != null )
			{
				$pre = true;
				eval( '$pre =(' . $itemVector["pre"] . ');' );
				
				if( $pre == false )
					continue;
			}
	
			foreach( $itemVector['vitem'] as $key => $r_item )
			{
				if( isset($r_item['exp']) )
				{
					$expRe = false;
					eval( '$expRe=(' . $r_item['exp']   . ');' );
					if( $expRe == true )
						continue;
					else
						$errorcode = 4;
				}
				else
				{
					$real_value = "";
					eval( '$is_set = isset($_'. $r_item['loc'] . '["' . $r_item['name'] . '"]);' );
					if( $is_set == false  )
					{
						if( $r_item['mandatory'] == 1 )
							HandleIllegalAccess();
						else
							continue;
					}
					else
					{	if( $r_item['loc'] == "GET" || 
							$r_item['loc'] == "POST" || 
							$r_item['loc'] == 'REQUEST' )
						{
							eval( '$real_value = $_' . $r_item['loc'] . '["' . $r_item['name'] . '"];' );
						}
						else
							$real_value = $r_item["name"] ;
					}
					
					if( $r_item['allow_empty'] == true && edp_is_empty_string($real_value) == true )
						continue;	
					
					$errorcode = 0;
						
					if( ( $r_item['size'] != null && v_size( $real_value, $r_item['size'][0], $r_item['size'][1], $r_item['type'] ) == false ) )
						$errorcode = 1;
						
					if( ( $r_item['validation'] != null && v_reg( $real_value, $r_item['validation'], $r_item['allow_empty'] ) == false ) )
						$errorcode = 2;
						
					if(	( $r_item['type'] != null && v_type( $real_value, $r_item['type']) == false ) )
						$errorcode = 3;
					
					if(	( $r_item['filter'] != null && v_filter( $real_value, $r_item['filter']) == false ) )
						$errorcode = 4;
				}
					
				if( $errorcode > 0)
				{
					if( $r_item['handler'] == ILLEGAL_ACCESS )
						HandleIllegalAccess();
					
					if(  $r_item['message'] != '' )
						$errmsg->add_session( "EVF", $r_item['message'] . "(CODE: $errorcode)" );
					
					$re = INVALID_PARA;
						
					if( $r_item['handler'] == CONTINUE_EXECUTE )
						continue;
						
					if( $r_item['handler'] == ABORT_EXECUTE )
						return $re;
				}
				
			
			}
			
		}
		return $re;
	}
	
	function v_filter( $value, $filter )
	{
		if( !function_exists($filter) )
			return false;
		
		return call_user_func( $filter, $value );
	}
	
	
	function HandleIllegalAccess()
	{
		global $page_base;
		$page_base = "illegal_access";
		
		edp_redirect_inner( $page_base, "" );
		exit();		
	}
	
	
	function v_reg( $real_value, $reg_expr, $allow_empty )
	{
		if( is_array($real_value) )
		{
			foreach( $real_value as $key => $value )
			{
				if( $allow_empty == true && trim($value) === "" )
					continue;
				if( preg_match( $reg_expr,  $value ) == 0 )
					return false;
			}
			return true;
		}
		else
		{
			if( $allow_empty == true && trim($real_value) === "" )
				return true;
				
			return preg_match( $reg_expr,  $real_value) == 0 ? false : true;
		}
	}
	
	
	function v_size( $p, $min, $max, $type )
	{
		$size = 0;
		
		if( is_array($p) )
			$size = count($p);
		else if( $type === VTYPE::INTEGER )
			$size = intval($p);
		else if( $type === VTYPE::FLOAT )
			$size = floatval($p);
		else if( $type === VTYPE::DOUBLE )
			$size = doubleval($p);
		else
			$size = strlen(trim($p));
			
		if( $min == null )
			return $size <= $max;
		else if( $max == null )
			return $size >= $min;
		else
			return $size >= $min && $size <= $max;
	}
	
	function v_type( $p, $type )
	{
		 
		switch( $type )
		{
			case VTYPE::STRING: return is_string($p);
			case VTYPE::NUMERIC: return is_numeric($p);
			case VTYPE::INTEGER: return is_numeric($p);
			case VTYPE::FLOAT: return is_numeric($p);
			case VTYPE::DOUBLE: return is_numeric($p);
			default: return false;
		}
	}
?>